﻿using System;
using System.Collections.Generic;
using System.Data;
using DTTB.Common;
using DTTB.Domain;

namespace DTTB.DAO.Base
{
    public class BaseDao<T> : AccessData, IBaseDao<T> //where T : new()
    {
        protected readonly Type ObjectType = typeof (T);

        /// <summary>
        /// Type of entity
        /// </summary>
        protected readonly string TypeFullName = typeof (T).FullName;

        protected readonly string TypeName = typeof (T).Name;


        public IList<T> GetAllEntities()
        {
            CommandText = "Select * From " + "TB_" + TypeName + " P Where P.IsDelete = 0 Order By P.ID DESC";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<T> list = CollectionHelper.ConvertTo<T>(table);
            return list;
        }

        public IList<T> GetAllEntities(string tableName)
        {
            CommandText = "Select * From " + tableName + " P Where P.IsDelete = 0 Order By P.ID DESC";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            IList<T> list = CollectionHelper.ConvertTo<T>(table);
            return list;
        }

        public bool AddNewEntity(T entity, ref int idReturn)
        {
            return Insert("TB_" + TypeName, entity, ref idReturn);
        }
        
        public bool AddNewEntity(T entity)
        {
            return Insert("TB_" + TypeName, entity);
        }

        public bool AddNewEntity(T entity, string tablename, ref int idReturn)
        {
            return Insert(tablename, entity, ref idReturn);
        }

        public bool AddNewEntity(T entity, string tablename)
        {
            return Insert(tablename, entity);
        }
    }
}